# -*- coding:utf-8 -*-
 
import tushare as ts
import os
import threading
import time
import akshare as ak
# from FxFetcher import *

def make_main_list():
    result = []
    with open('main_config.ini','r') as fd:
        for line in fd.readlines():
            result.append(str(line).replace('\n','').replace('\r',''))
    return result

def make_stock_list():
    result = []
    with open('config.ini','r') as fd:
        for line in fd.readlines():
            result.append(str(line).replace('\n','').replace('\r',''))
    return result

def white_stock(mainlist,stocklist):
    fd = open("stock.txt","w")
    i = os.system("clear")                                          # 清屏操作
    result = "========================Main========================\n"
    df = ts.get_realtime_quotes(mainlist)
    for i in range(len(mainlist)):
        result += df['code'][i] + "  " + str(df['name'][i]) + "  "  + str(df['price'][i]) + "  " + str(round((float(df['price'][i]) - float(df['pre_close'][i])) / float(df['pre_close'][i]) * 100, 2)) + "%" + "  "
        result += "\n"
    result += "========================Stock========================\n"
    df = ts.get_realtime_quotes(stocklist)
    for j in range(len(stocklist)):
        codenum = df['code'][j]
        result += df['code'][j] + "  " + str(df['name'][j]) + "  "  + str(df['price'][j]) + "  " + str(round((float(df['price'][j]) - float(df['pre_close'][j])) / float(df['pre_close'][j]) * 100, 2)) + "%" 
        result += "\n"

    try:
        result += "=========================FX========================\n"
        fx_spot_quote_df = ak.fx_spot_quote()
        result += str(fx_spot_quote_df.iloc[:4])
        result += "\n"
        fx_pair_quote_df = ak.fx_pair_quote()
        result += str(fx_pair_quote_df.iloc[6:8])
        result += "\n"
    except:
        pass

    fd.write(result)
    fd.close()

    # global timer
    # timer = threading.Timer(10.0, white_stock, [mainlist,stocklist])
    # timer.start()


# def get(mainlist,stocklist):
#     i = os.system("clear")                                          # 清屏操作
#     print ("========================Main========================")
#     df = ts.get_realtime_quotes(mainlist)
#     for i in range(len(mainlist)):
#         print(df['code'][i] + "  " + str(df['name'][i]) + "  "  + str(df['price'][i]) + "  " + str(round((float(df['price'][i]) - float(df['pre_close'][i])) / float(df['pre_close'][i]) * 100, 2)) + "%" + "  ")
#     print ("========================Stock===============5========")
#     df = ts.get_realtime_quotes(stocklist)
#     for j in range(len(stocklist)):
#         codenum = df['code'][j]
#         resultstr = df['code'][j] + "  " + str(df['name'][j]) + "  "  + str(df['price'][j]) + "  " + str(round((float(df['price'][j]) - float(df['pre_close'][j])) / float(df['pre_close'][j]) * 100, 2)) + "%" 
#         #resultstr += "       | DeltaMA5 = " + str(round(float(df['price'][j])-ma5result[codenum],2))
#         print (resultstr)

#     try:
#         print ("=========================FX========================")
#         fx_spot_quote_df = ak.fx_spot_quote()
#         print(fx_spot_quote_df.iloc[:4])
#         fx_pair_quote_df = ak.fx_pair_quote()
#         print(fx_pair_quote_df.iloc[6:8])
#     except:
#         pass

#     global timer
#     timer = threading.Timer(10.0, get, [mainlist,stocklist])
#     timer.start()
 
if __name__ == "__main__":
    mainlist = make_main_list() 
    resultlist = make_stock_list()
    timer = threading.Timer(1.0, get, [mainlist,resultlist])
    timer.start()
